package com.example.demo.repository;

import com.example.demo.entity.Member;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 会员数据访问层
 */
@Repository
public interface MemberRepository extends JpaRepository<Member, Long> {
    
    Optional<Member> findByMemberCode(String memberCode);
    
    Optional<Member> findByPhone(String phone);
    
    Optional<Member> findByEmail(String email);
    
    List<Member> findByMemberLevel(Member.MemberLevel memberLevel);
    
    List<Member> findByStatus(Member.MemberStatus status);
    
    @Query("SELECT m FROM Member m WHERE m.memberName LIKE %:keyword% OR m.phone LIKE %:keyword%")
    List<Member> findByKeyword(@Param("keyword") String keyword);
    
    @Query("SELECT m FROM Member m WHERE m.totalPoints >= :minPoints")
    List<Member> findByMinPoints(@Param("minPoints") Integer minPoints);
    
    boolean existsByMemberCode(String memberCode);
    
    boolean existsByPhone(String phone);
    
    boolean existsByEmail(String email);
}
